package com.yxzx.ebs.controller.front;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yxzx.common.YxzxResult;
import com.yxzx.ebs.entity.EbsCourse;
import com.yxzx.ebs.entity.vo.EbsChapterVo;
import com.yxzx.ebs.entity.vo.EbsCourseWebVo;
import com.yxzx.ebs.service.EbsChapterService;
import com.yxzx.ebs.service.EbsCourseService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Copyright (C), 2018-2020
 * FileName: EbsFrontTeacherController
 * Author:   kongfanyu
 * Date:     2020/8/17 00:08
 */
@Api(tags = "课程管理前台业务控制器")
@RestController
@CrossOrigin
@RequestMapping("/eduservice/front/course")
public class EbsFrontCourseController {
    @Autowired
    private EbsCourseService courseService;

    @Autowired
    private EbsChapterService chapterService;

    @ApiOperation(value = "分页查询课程")
    @GetMapping("{page}/{limit}")
    public YxzxResult getPageList(@ApiParam(name = "page",value = "当前页",   required = true) @PathVariable Long page,
                                  @ApiParam(name = "limit",value = "每页大小",required = true) @PathVariable Long limit){
        QueryWrapper<EbsCourse> queryWrapper = new QueryWrapper<>();
        queryWrapper.orderByDesc("gmt_modified");
        Page<EbsCourse> courseIPage = new Page<>(page, limit);
        courseService.page(courseIPage,queryWrapper);

        List<EbsCourse> records = courseIPage.getRecords();
        long current = courseIPage.getCurrent();
        long pages = courseIPage.getPages();
        long size = courseIPage.getSize();
        long total = courseIPage.getTotal();
        boolean hasNext = courseIPage.hasNext();
        boolean hasPrevious = courseIPage.hasPrevious();

        Map<String, Object> map = new HashMap<String, Object>();
        map.put("items", records);
        map.put("current", current);
        map.put("pages", pages);
        map.put("size", size);
        map.put("total", total);
        map.put("hasNext", hasNext);
        map.put("hasPrevious", hasPrevious);
        return YxzxResult.ok().data(map);
    }

    @ApiOperation(value = "根据ID查询课程详情")
    @GetMapping("{courseId}")
    public YxzxResult getById(@ApiParam(name = "courseId",value = "课程ID",required = true)
                                  @PathVariable String courseId){
        //1.查询课程详情和讲师信息
        EbsCourseWebVo ebsCourseWebVo = courseService.selectInfoWebById(courseId);
        //2.查询课程的章节信息
        List<EbsChapterVo> vedioList = chapterService.getChapterAndVedioList(courseId);
        //3.返回数据
        return YxzxResult.ok().data("course",ebsCourseWebVo).data("vedioList",vedioList);
    }
}
